<?php

if(isset($_POST['submit'])) {
    $json = file_get_contents("data.json");
    $data = json_decode($json);
    $map = array();
    $i = 1;
    foreach($data as $k=>$v) {
        $name = explode(" ", $k)[1];
        $item = array();
        $item['name'] = $name;
        $item['key'] = "tizhi{$i}";
        array_push($map, $item);
        $i++;
    }

    // 计算
    foreach($map as &$v) {
        $name = $v['name'];
        $tizhi = $v['key'];
        //echo $name . '-' . $tizhi . "<br/>";
        $score = 0;
        $tiaoshu = 0;
        foreach($_POST as $k=>$v2) {
            if(strpos($k, $tizhi)!==false) {
                //echo $v2 . "<br/>";
                $score += $v2;
                $tiaoshu++;
            }
        }
        $v['score'] = $score;
        $v['tiaoshu'] = $tiaoshu;
        if($tiaoshu != 0 ) {
            $v['zhf'] = (int)(($score-$tiaoshu)/($tiaoshu*4)*100);
        } else {
            $v['zhf'] = 0;
        }
        
    }

    // 判断平和质(是)
    $tap = "否";
    $pinghe = "否";
    foreach($map as &$v) {
        if($v['name'] == "平和质") {
            if($v['zhf'] >= 60) {
                $tap = "是";
            }
        }
    }

    if($tap == "是") {
        $flag = 0;
        foreach($map as $v) {
            
            if($v['name'] != "平和质") {
                if($v['zhf'] >= 30) {
                    echo $v['name'];die;
                    $flag = 1;
                }
            }
        }
        if($flag!=1) {
            $pinghe = "是";
            
        }
    }

    if($pinghe == "否") {
        $flag = 0;
        foreach($map as $v) {
            if($v['name'] != "平和质") {
                if($v['zhf'] >= 40) {
                    $flag = 1;
                }
            }
        }
        if($flag!=1) {
            $pinghe = "基本是";
        }
    }

    echo "平和质：" . $pinghe . "<br/>";
    // 判断平和质完成

    // 偏颇质
    $pianpo = array();
    foreach($map as $v) {
        if($v['name'] != "平和质") {
            if($v['zhf'] >= 40) {
                $v['tap'] = "是";
            } elseif($v['zhf'] >= 30) {
                $v['tap'] = "倾向否";
            } else {
                $v['tap'] = "否";
            }
            array_push($pianpo, $v);
        }
    }

    foreach($pianpo as $v) {
        echo "{$v['name']}：{$v['tap']}<br/>";
    }

    die;
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <style>
        .box {
            width: 1200px;
            margin: 0 auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table tr td {
            border: 1px solid #999;
            padding: 10px;
        }
        .col1 {
            width: 50%;
        }
    </style>
</head>
<body>
    <form action="index.php" method="POST">
        <div class="box">
            <p>
                <b>
                    1.判定方法<br>
                    回答《中医体质分类与判定表》中的全部问题，每一问题按5级评分，计算原始分和转化分，依标准判定体质类型。<br>
                    原始分＝各个条目的分值相加。<br>
                    转化分数＝［（原始分-条目数）/（条目数×4）］×100<br>
                    2.判定标准<br>
                    平和质为正常体质，其他8种体质为偏颇体质。判定标准见下：<br>
                    <img src="http://www.sojump.cn/Upload/UserUpload/2014-06-01/fb5b9d3e-2787-4957-94fa-a1fd7d6ebdf5.png" alt="" viewer="1"><br>
                    3.示例<br>
                    示例1：某人各种体质类型转化分如下：平和质75分，气虚质56分，阳虚质27分，阴虚质25分，痰湿质12分，湿热质15分，血瘀质20分，气郁质18分，特禀质10分。根据判定标准，虽然平和质转化分≥60分，但其他8种体质转化分并未全部＜40分，其中气虚质转化分≥40分，故此人不能判定为平和质，应判定为气虚质。<br>
                    示例2：某人各种体质类型转化分如下：平和质75分，气虚质16分，阳虚质27分，阴虚质25分，痰湿质32分，湿热质25分，血瘀质10分，气郁质18分，特禀质10分。根据判定标准，平和质转化分≥60分，且其他8种体质转化分均＜40分，可判定为基本是平和质，同时，痰湿质转化分30～39分之间，可判定为痰湿质倾向，故此人最终体质判定结果基本是平和质，有痰湿质倾向。<br>
                </b>
            </p>
            <p>
                <?php
                    $json = file_get_contents("data.json");
                    $data = json_decode($json);
                    $map = array();
                    $i = 1;
                    foreach($data as $k=>$v):
                        $tizhi = "tizhi".$i++;
                ?>
            </p>
            <table>
                <tr>
                    <td colspan="6">
                        <?php echo $k; ?>
                    </td>
                </tr>
                <tr>
                    <td class='col1'>
                        <input type="hidden" name="">
                    </td>
                    <td>没有（1分）</td>
                    <td>很少（2分）</td>
                    <td>有时（3分）</td>
                    <td>经常（4分）</td>
                    <td>总是（5分）</td>
                </tr>
                <?php
                    $j = 1;
                    foreach($v as $v2):
                        $sub = $j++;
                        // 判断是否需要分数逆序
                        if(strpos($v2,"＊")) {
                            $flag = "逆序";
                        } else {
                            $flag = "顺序";
                        }
                ?>
                <tr>
                    <td class='col1'>
                        <?php 
                            echo $v2; 
                        ?>
                    </td>
                    <?php
                        if($flag == "顺序"):
                    ?>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="1" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="2" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="3" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="4" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="5" />
                    </td>
                    <?php
                        else:
                    ?>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="5" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="4" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="3" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="2" />
                    </td>
                    <td>
                        <input type="radio" name="<?php echo "{$tizhi}_{$sub}"; ?>" value="1" />
                    </td>
                    <?php
                        endif;
                    ?>
                </tr>
                <?php endforeach; ?>
            </table>
            <?php
                endforeach;
            ?>
            <p style="text-align: center;">
            （注：标有*的条目需逆向计分，即1→5,2→4,3→3,4→2,5→1，再用公式转化分）
            </p>
            <p style='text-align: center;'>
                <input type="submit" name="submit" value="提交">
            </p>
        </div>
    </form>
    
</body>
</html>